#!/anaconda3/envs/Script/bin python3.8
# -*- coding: utf-8 -*-
# ---
# @Software: PyCharm
# @File: log_service.py
# @Author: AllenFu
# @Institution: NCHU, China
# @E-mail: nchufujianjian@126.com
# @Site: 日志服务
# @Time: 11月 27, 2020
# ---
import time

from app import dBSession
from app.models.model import HtLog


class LogService:

    def add(self, data:str, type:int=1, level:int=1)->bool:
        '''
        添加日志
        :param data 日志数据
        :param type
        :param level
        :return
        '''

        tmp_data = {
            'data': data,
            'type':type,
            'level':level,
            'create_time': int(time.time()),
        }
        try:
            htlog = HtLog(**tmp_data)
            dBSession.add(htlog)
            dBSession.flush()
            logid= htlog.id
            dBSession.commit()
            return logid
        except Exception as e:
            dBSession.rollback()
            return 0
